<?php 
$xml = simplexml_load_file(__DIR__.'/../src/main/resources/org/dicr/radius/dict/default-dictionary.xml');

$vnd = new stdClass();
$vnd->name = "RFC";
$vnd->code = "0";
$vnd->attrs = array();
$data = array("0" => $vnd);

foreach ($xml->vendor as $vendor) {
	$vnd = new stdClass();
	$vnd->code = (string)$vendor['code'];
	$vnd->name = (string)$vendor['name'];
	$vnd->attrs = array();	
	$data[$vnd->code] = $vnd;
}

foreach ($xml->attribute as $attribute) {
	$attr = new stdClass();
	$attr->vendor = (string)$attribute['vendor'];
	$attr->code = (string)$attribute['code'];
	$attr->name = (string)$attribute['name'];
	$attr->type = (string)$attribute['type'];
	$attr->encoding = (string)$attribute['encoding'];
	$attr->values = array();
	foreach ($attribute->value as $value) {
		$val = new stdClass();
		$val->code = (string)$value['code'];
		$val->name = (string)$value['name'];
		$attr->values[] = $val;
	}
	$data[$attr->vendor]->attrs[] = $attr;
}

$xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "default-dictionary.dtd">
<dictionary name="radius"></dictionary>');
foreach ($data as $vnd) {
	$vendor = $xml->addChild('vendor');
	$vendor['code'] = $vnd->code;
	$vendor['name'] = $vnd->name;
	foreach ($vnd->attrs as $attr) {
		$attribute = $vendor->addChild('attribute');
		$attribute['code'] = $attr->code;
		$attribute['name'] = $attr->name;
		$attribute['type'] = $attr->type;
		$attribute['encoding'] = $attr->encoding;
		foreach ($attr->values as $val) {
			$value = $attribute->addChild('value');
			$value['name'] = $val->name;
			$value['data'] = $val->code;
		}
	}
} 
echo $xml->asXML();

